package tools

import (
	"bytes"
	uuid "github.com/satori/go.uuid"
	"log"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
	"time"
)

func init() {
	rand.Seed(time.Now().Unix())
}

//生成UUID
func GetUUID() string {
	return strings.ToUpper(uuid.Must(uuid.NewV4()).String())
}

//字符串拼接
func StringsJoin(str ...string) string {
	var b bytes.Buffer
	strLen := len(str)
	if strLen == 0 {
		return ""
	}
	for i := 0; i < strLen; i++ {
		b.WriteString(str[i])
	}

	return b.String()
}

//返回当前工作目录的绝对路径
func GetAbsPath() string {
	path, err := filepath.Abs(filepath.Dir(os.Args[0])) //作为服务时使用
	//path, err := os.Getwd()
	if err != nil {
		log.Fatalln("GetAbsPath() error: ", err)
	}
	path = filepath.ToSlash(path)
	return StringsJoin(path, "/")
}

//根据文件路径创建对应的文件夹
func MkdirAllFile(path, appPath string) string {
	//filePath文件路径，fileName文件名
	filePath, fileName := filepath.Split(path)
	//日志文件的绝对路径
	filePath = StringsJoin(appPath, filePath)
	//panic日志文件的绝对路径+文件名
	path = StringsJoin(filePath, fileName)
	//根据文件路径创建对应的文件夹
	err := os.MkdirAll(filePath, os.ModePerm)
	if err != nil {
		log.Fatalln("panicFile failed to MkdirAll(): ", err)
	}
	return path
}
